//
// Created by lz on 3/4/21.
//

#ifndef MYWEBLIB_NET_ACCEPTOR_H_
#define MYWEBLIB_NET_ACCEPTOR_H_

#include "net/Channel.h"
#include "net/Socket.h"

#include <functional>


namespace lzweb
{
	namespace net
	{
		class EventLoop;
		class InetAddress;

		///
		/// Acceptor of incoming TCP connections.
		///
		class Acceptor : noncopyable
		{
		 public:
			using NewConnectionCallback = std::function<void(int sockfd, const InetAddress&)>;

			Acceptor(EventLoop* loop, const InetAddress& listenAddr, bool reuseport);
			~Acceptor();

			void setNewConnectionCallback(const NewConnectionCallback& cb)
			{
				newConnectionCallback_ = cb;
			}

			void listen();

			bool listening() const { return listening_; }

			// Deprecated, use the correct spelling one above.
			// Leave the wrong spelling here in case one needs to grep it for error messages.
			// bool listenning() const { return listening(); }

		 private:
			void handleRead();

		 	EventLoop* loop_;
		 	Socket acceptSocket_;
		 	Channel acceptChannel_;
		 	NewConnectionCallback newConnectionCallback_;
		 	bool listening_;
		 	int idleFd_;   // idle -> 闲置的， 该成员用来处理EMFILE错误的情况,当打开的文件描述符太多时，可以用此fd为一个中间媒介将溢出的fd丢掉（关闭）
		};
	}// namespace lzweb
}// namespace lzweb


#endif //MYWEBLIB_NET_ACCEPTOR_H_
